package com.woquyin.controller.api;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.json.FastJson;
import com.woquyin.interceptor.ApiAuthInterceptor;
import com.woquyin.interceptor.ParamInterceptor;
import com.woquyin.model.SecUser;
import com.woquyin.model.SecUserAddress;
import com.woquyin.vo.ReturnData;
import com.woquyin.vo.address.AddressAddVo;

@Before(ApiAuthInterceptor.class)
public class AddressController extends Controller {
	
	public void list() {
		SecUser vo = (SecUser) getSession().getAttribute("userInfo");
		setAttr("addressList", SecUserAddress.dao.query(vo.getId()));
		setAttr("userInfo", vo);
		renderJsp("recAddress.jsp");
	}

	public void add() {
		String jsonString = getAttrForStr(ParamInterceptor.REQUEST_JSON);
		AddressAddVo vo = FastJson.getJson().parse(jsonString, AddressAddVo.class);
		renderJson(SecUserAddress.dao.addAddressService(vo, ((SecUser)getSession().getAttribute("userInfo")).getId()));
	}

	public void update() {
		String jsonString = getAttrForStr(ParamInterceptor.REQUEST_JSON);
		AddressAddVo vo = FastJson.getJson().parse(jsonString, AddressAddVo.class);
		renderJson(SecUserAddress.dao.updateAddressService(vo));
	}

	public void get() {
		String addressId = getPara("id");
		ReturnData<SecUserAddress> returnData = (ReturnData)SecUserAddress.dao.getAddressService(addressId);
		renderJson(returnData);
	}

	public void delete() {
		String addressId = getPara("id");
		renderJson(SecUserAddress.dao.deleteAddressService(addressId));
	}
	
	public void updateDefaultAddress(){
		String addressId = getPara("addressId");
		renderJson(SecUserAddress.dao.changeDefaultAddressService(addressId, ((SecUser)getSession().getAttribute("userInfo")).getId()));
	}
}
